package com.lg.desireplayer.member;

import java.net.MalformedURLException;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lg.desireplayer.R;
import com.lg.desireplayer.db.BandListMaker;
import com.lg.desireplayer.db.MyListMaker;
import com.lg.desireplayer.main.Main;

public class Login extends Activity {

	Button login_btn_memberSignup, login_btn_memberLogin,
			login_btn_nonMemberLogin;
	EditText login_Edit_id, login_Edit_pw;
	final static int MEMBER_NORMAL = 0;
	final static int MEMBER_BAND = 1;
	final static int NON_MEMBER = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// 회원 가입
		login_btn_memberSignup = (Button) findViewById(R.id.Login_Button_signUp);
		login_btn_memberSignup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(Login.this, Signup.class);
				startActivity(intent);
				overridePendingTransition(R.anim.leftin, R.anim.leftout);

			}
		});

		// 비회원 접속
		login_btn_nonMemberLogin = (Button) findViewById(R.id.Login_Button_nonMember);
		login_btn_nonMemberLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// 메인 화면으로 intent!
				Intent intent = new Intent(Login.this, Main.class);

				SharedPreferences prefs = getSharedPreferences("Desire",
						MODE_PRIVATE);
				SharedPreferences.Editor editor = prefs.edit();
				editor.putInt("LoginMode", NON_MEMBER);
				editor.commit();

				startActivity(intent);
				overridePendingTransition(R.anim.leftin, R.anim.leftout);
				//동현추가
				finish();
			}
		});

		login_Edit_id = (EditText) findViewById(R.id.Login_Edit_ID);
		login_Edit_pw = (EditText) findViewById(R.id.Login_Edit_PW);

		// 회원 접속
		login_btn_memberLogin = (Button) findViewById(R.id.Login_Button_member);
		login_btn_memberLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (login_Edit_id.getText().toString().equals("")) {
					Toast.makeText(Login.this, "아이디를 입력해 주세요.",
							Toast.LENGTH_SHORT).show();
				} else if (login_Edit_pw.getText().toString().equals("")) {
					Toast.makeText(Login.this, "패스워드를 입력해 주세요.",
							Toast.LENGTH_SHORT).show();
				} else {
					String resultId = null;
					String resultCheck = null;
					String resultIndex = null;
					StringBuffer str = new StringBuffer(
							"http://192.168.0.131:8080/com.lg.project.member.cmd.MemberLoginCmd.vcc?username=");
					str.append(login_Edit_id.getText().toString());
					str.append("&password=");
					str.append(login_Edit_pw.getText().toString());

					try {
						URL url = new URL(str.toString());

						XmlPullParserFactory factory = XmlPullParserFactory
								.newInstance();
						XmlPullParser parser = factory.newPullParser();
						parser.setInput(url.openStream(), "utf-8");

						int eventType = parser.getEventType();
						while (eventType != XmlPullParser.END_DOCUMENT) {
							switch (eventType) {
							case XmlPullParser.START_DOCUMENT:
								break;
							case XmlPullParser.START_TAG:
								if (parser.getName().equals("username")) {
									resultId = parser.nextText();
								} else if (parser.getName().equals("isband")) {
									resultCheck = parser.nextText();
								} else if (parser.getName().equals("id")) {
									resultIndex = parser.nextText();
									Log.i("URL", resultIndex);
								}
								break;
							case XmlPullParser.END_TAG:
								break;
							}
							eventType = parser.next();
						}

					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					}

					if (resultId == null) {
						Toast.makeText(Login.this, "회원 정보가 일치하지 않습니다.",
								Toast.LENGTH_SHORT).show();
					} else {

						Toast.makeText(Login.this, resultId + " 님 환영합니다.",
								Toast.LENGTH_SHORT).show();

						// 메인으로 Intent!
						Intent intent = new Intent(Login.this, Main.class);

						if (resultCheck.equals("0")) {
							SharedPreferences prefs = getSharedPreferences(
									"Desire", MODE_PRIVATE);
							SharedPreferences.Editor editor = prefs.edit();
							editor.putInt("LoginMode", MEMBER_NORMAL);
							editor.putInt("LoginID",
									Integer.parseInt(resultIndex));
							editor.commit();
						} else if (resultCheck.equals("1")) {
							SharedPreferences prefs = getSharedPreferences(
									"Desire", MODE_PRIVATE);
							SharedPreferences.Editor editor = prefs.edit();
							editor.putInt("LoginMode", MEMBER_BAND);
							editor.putInt("LoginID",
									Integer.parseInt(resultIndex));
							editor.commit();
						}

						startActivity(intent);
						overridePendingTransition(R.anim.leftin, R.anim.leftout);
						//동현추가
						finish();
					}
				}

			}
		});

	}
}
